<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm" @submit.native.prevent>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/deal/home/dealBaseHome',query:{id:form.dealId,companyId:form.companyId}}"
                class="link_a"
              >{{ form.dealName }}
              </router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司名称')">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/company/base/companyBaseHome',query:{companyId:form.companyId}}"
                class="link_a"
              >{{ form.companyName }}
              </router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" placeholder="请输入标题" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="deal_process" :business-id="form.dealId" :btn-show="!otherBtnHide" />
    </el-form>
  </div>
</template>

<script>
import { saveDealWfGeneral, getDealWfGeneral, getByWfNodeId } from '@/api/deal/form/dealWfGeneral'

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        actTitle: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true,
      // 是否隐藏保存按钮
      otherBtnHide: false
    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDealWfGeneral(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        getByWfNodeId(data.wfNodeId).then(response => {
          this.form = response.data
          this.form.fileList = data.fileList
          this.form.actTitle = data.actTitle
          this.loading = false
        })
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: this.$route.query.id,
        companyId: undefined,
        actTitle: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    /** 提交按钮 */
    submitForm: function() {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true

            // 保存数据
            saveDealWfGeneral(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                this.loading = false
                resolve(response.data.id)
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject(response)
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            reject()
            this.locationError(document)
          }
        })
      })
    }

  }

}
</script>
